package com.kipfer.movetest;
//What up yo
import java.util.ArrayList;


import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.util.Log;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

public class Board extends SurfaceView implements SurfaceHolder.Callback {

	// -----------------------------GLOBAL VARIABLES
	// -----------------------------------------------
	private MoveTestThread _thread;
	private int _x; // touch x position
	private int _y; // touch y position
	private boolean touchPadVisable = false;
	private boolean moveRight = false;
	private boolean moveRightFast = false;
	private boolean moveLeft = false;
	private boolean moveLeftFast = false;
	private boolean moveUp = false;
	private boolean moveDown = false;
	private boolean start = true;
	private ArrayList<Character> _blocks = new ArrayList<Character>();
	private ArrayList<Character> _ramp = new ArrayList<Character>();
	private boolean onground;
	private boolean blockedbottom;
	private boolean blockedtop;
	private boolean blockedright;
	private boolean blockedleft;
	private int count;
	private int sp;
	private int friction = 0;
	private int gravity = 0;
	private int upsp = 7;

	// ------------------------------Character Variables
	// -------------------------------------------------
	Character hen = new Character(BitmapFactory.decodeResource(getResources(),R.drawable.chickenleft));
	Character block = new Character(BitmapFactory.decodeResource(getResources(), R.drawable.block));
	Character block2 = new Character(BitmapFactory.decodeResource(getResources(), R.drawable.block));
	Character block3 = new Character(BitmapFactory.decodeResource(getResources(), R.drawable.block));
	Character block4 = new Character(BitmapFactory.decodeResource(getResources(), R.drawable.block));
	Character block5 = new Character(BitmapFactory.decodeResource(getResources(), R.drawable.block));
	Character block6 = new Character(BitmapFactory.decodeResource(getResources(), R.drawable.block));
	Character frontedge = new Character(BitmapFactory.decodeResource(getResources(), R.drawable.block));
	
	Matrix matrix = new Matrix();
	
	
	

	public Board(Context context) {
		super(context);
		getHolder().addCallback(this);
		_thread = new MoveTestThread(getHolder(), this);
		setFocusable(true);
		_blocks.add(block);
		_blocks.add(block2);
		_blocks.add(block3);
		_blocks.add(block4);
		_blocks.add(block5);
		_blocks.add(block6);
		_blocks.add(frontedge);
	}

	// ------------------------------ ON DRAW
	// --------------------------------------------------
	@Override
	protected void onDraw(Canvas canvas) {
		Bitmap bitmap;
		Character.Coordinates coords;

		canvas.drawColor(Color.GRAY);

		if (start) {

			hen.getCoordinates().setX(getWidth() / 2);
			hen.getCoordinates().setY(getHeight() / 2);
			block.getCoordinates().setX(getWidth()/2);
			block.getCoordinates().setY(getHeight()-block.getGraphic().getHeight());
			block2.getCoordinates().setX(getWidth()/2 + block2.getGraphic().getWidth());
			block2.getCoordinates().setY(getHeight()-block.getGraphic().getHeight()*2);
			block3.getCoordinates().setX(getWidth()/2 - block2.getGraphic().getWidth());
			block3.getCoordinates().setY(getHeight()-block.getGraphic().getHeight()*2);
			block4.getCoordinates().setX(getWidth()/2 + block2.getGraphic().getWidth()*2);
			block4.getCoordinates().setY(getHeight()-block.getGraphic().getHeight()*2);
			block5.getCoordinates().setX(getWidth()/2 + block2.getGraphic().getWidth()*3);
			block5.getCoordinates().setY(getHeight()-block.getGraphic().getHeight());
			block6.getCoordinates().setX(getWidth()/2 - block2.getGraphic().getWidth()*3);
			block6.getCoordinates().setY(getHeight()-block.getGraphic().getHeight());
			frontedge.getCoordinates().setX(0-block.getGraphic().getWidth());
			frontedge.getCoordinates().setY(getHeight());
			
			matrix.postScale( ( ((float) 3 ) / block2.getGraphic().getWidth()), (((float)3) /block.getGraphic().getHeight() ));
			Bitmap pxblock = Bitmap.createBitmap(block.getGraphic(), 0, 0, block.getGraphic().getWidth(),block.getGraphic().getHeight(), matrix, true);
			
			for(int i = 0; i<200;i++){
				Character ramppart = new Character(pxblock);
				ramppart.getCoordinates().setX(block5.getCoordinates().getX()+(block5.getGraphic().getWidth()*2)-i+3);
				ramppart.getCoordinates().setY(getHeight()-ramppart.getGraphic().getHeight()-i+5);
				_ramp.add(ramppart);
				_blocks.add(ramppart);
			}
			
//			for(int i = 0; i<100;i++){
//				Character ramppart = new Character(pxblock);
//				ramppart.getCoordinates().setX(block5.getCoordinates().getX()+(block5.getGraphic().getWidth())-i-4);
//				ramppart.getCoordinates().setY(block5.getCoordinates().getY()-ramppart.getGraphic().getHeight()-i+1);
//				_ramp.add(ramppart);
//				_blocks.add(ramppart);
//			}
			
			
			
			
			
			start = false;
		}

		bitmap = hen.getGraphic();
		coords = hen.getCoordinates();
		canvas.drawBitmap(bitmap, coords.getX(), coords.getY(), null);
		bitmap = block.getGraphic();
		coords = block.getCoordinates();
		canvas.drawBitmap(bitmap, coords.getX(), coords.getY(), null);
		bitmap = block2.getGraphic();
		coords = block2.getCoordinates();
		canvas.drawBitmap(bitmap, coords.getX(), coords.getY(), null);
		bitmap = block3.getGraphic();
		coords = block3.getCoordinates();
		canvas.drawBitmap(bitmap, coords.getX(), coords.getY(), null);
		bitmap = block4.getGraphic();
		coords = block4.getCoordinates();
		canvas.drawBitmap(bitmap, coords.getX(), coords.getY(), null);
		bitmap = block5.getGraphic();
		coords = block5.getCoordinates();
		canvas.drawBitmap(bitmap, coords.getX(), coords.getY(), null);
		bitmap = block6.getGraphic();
		coords = block6.getCoordinates();
		canvas.drawBitmap(bitmap, coords.getX(), coords.getY(), null);
		
		for(Character ramp: _ramp){
			bitmap = ramp.getGraphic();
			coords = ramp.getCoordinates();
			canvas.drawBitmap(bitmap, coords.getX(), coords.getY(), null);
		}
		

		if (touchPadVisable) {
			Bitmap touchPad = BitmapFactory.decodeResource(getResources(),
					R.drawable.pad);
			canvas.drawBitmap(touchPad, _x - touchPad.getWidth() / 2, _y
					- touchPad.getHeight() / 2, null);
		}

	}
	
	public void checkforblock(){
		
		for(Character block : _blocks) {
			int check = checkCollisionBottom(hen,block);
			if(check == 1){
				blockedbottom = true;
				
				hen.getCoordinates().setY(block.getCoordinates().getY()-hen.getGraphic().getHeight());
				
				
			}
			check = checkCollisionTop(hen,block);
			 if(check == 4){
					blockedtop = true;
				}
			 check = checkCollisionSides(hen,block);
			if(check == 2){
				blockedright = true;
				
				onground = true;
			}
			if(check == 3){
				blockedleft = true;
			
			}
			
		}
	}
	
	public void reset(){
		blockedbottom = false;
		blockedright = false;
		blockedleft = false;
		blockedtop = false;
	}
	

	public void updatePhysics() {
	
		
		Character.Coordinates coord;
		Character.Coordinates blockcoord;
		Character.Speed speed;
		// -----------see if they are on the ground falling or jumping
		coord = hen.getCoordinates();
		speed = hen.getSpeed();
		
		if (coord.getY() < getHeight() - hen.getGraphic().getHeight() && !blockedbottom) {
		onground = false;
		} else {
			onground = true;
		}
		
		
		if (moveRight) {
			if(!blockedright){
			sp = 1;
			friction = 0;}
			else{
				sp = 0;
			}
		}
		if (moveLeft) {
			if(!blockedleft){
				
			
			sp = -1;
			friction = 0;
			}else{
				sp=0;
			}
		}
		if (moveRightFast) {
			if(!blockedright){
			if (sp < 5){
				if(!(friction < 7)){
				sp = sp +1;
				friction = 0;
				}else{
					friction = friction+1;
				}
			}}else{
				sp= 0;
			}
			
		}
		if (moveLeftFast) {
			if(!blockedleft){
			if(sp > -5){
				if(!(friction < 7)){
				sp = sp -1;
				friction = 0;
				}else{
					friction = friction+1;
				}
			}}else{
				sp=0;
			}
		}
		//-------------Friction ----------------------------------------------------
		if(!(friction < 5)){
		if(!moveRight && !moveLeft &&!moveRightFast &&!moveLeftFast&& (onground|| blockedbottom)){
			{
				if(sp != 0 && sp < 0){
				sp= sp + 1;	
				}
				if(sp != 0 && sp > 0){
				sp= sp - 1;
				}
				friction = 0;
			}
		}
			}else{
				friction = friction +1;
		
		}
		if((frontedge.getCoordinates().getX() > 0 &&sp<0)||(sp > 0 && hen.getCoordinates().getX()<getWidth()/2)||(hen.getCoordinates().getX() > getWidth()/2 && hen.getCoordinates().getX()< getWidth() - getWidth()/2)){
		coord.setX(coord.getX() + sp);}
		else{
			for(Character bl : _blocks){
				blockcoord = bl.getCoordinates();
				blockcoord.setX(blockcoord.getX()-sp);
				
			}
		}
		
		
		//---------------------- GRAVITY -----------------------------------------
		
		if (!moveUp && !onground && !blockedbottom) {
			if(gravity == 10){
				if(upsp < 7){
				upsp = upsp +1;
				gravity = 0;
				}
			}else{
				gravity = gravity + 1;
			}
			if(coord.getY()+hen.getGraphic().getHeight() + upsp < getHeight()){ 
			coord.setY(coord.getY() + upsp);
			}else{
				coord.setY(getHeight() -  hen.getGraphic().getHeight());
			}
				
		}
		
		
		if (moveUp) {
			
			if(count < 30 && !blockedtop){
				
			coord.setY(coord.getY() - upsp);
				
			if(gravity == 10){
				upsp = upsp- 1;
				gravity = 0;
			}else{
				gravity = gravity +1;
			}
			count = count +1;
			}else{
				upsp = 7;
				gravity = 0;
				count = 0;
				moveUp = false;
			}
		} 
		
	}
	private int checkCollisionTop(Character first, Character second) {
		int retValue = 0;
		int firstwidth = first.getGraphic().getWidth();
		int firstheight = first.getGraphic().getHeight();
		int secondwidth = second.getGraphic().getWidth();
		int secondheight = second.getGraphic().getHeight();
		int firstYRangeStart = first.getCoordinates().getY();
		int firstYRangeEnd = first.getCoordinates().getY() + firstheight;
		int firstXRangeStart = first.getCoordinates().getX();
		int firstXRangeEnd = first.getCoordinates().getX() + firstwidth;
		int secondYRangeStart = second.getCoordinates().getY();
		int secondYRangeEnd = second.getCoordinates().getY()+ secondheight;
		int secondXRangeStart = second.getCoordinates().getX();
		int secondXRangeEnd = second.getCoordinates().getX()+ secondwidth;
	

		
//		if(secondXRangeStart <= firstXRangeEnd && firstXRangeStart < secondXRangeEnd-secondwidth/2){
//			//Log.i("Blocked",""+firstYRangeStart+"/"+firstYRangeEnd+"-"+secondYRangeStart+"/"+secondYRangeEnd);
//			if ((firstYRangeStart <= secondYRangeStart && firstYRangeEnd >= secondYRangeStart)
//					|| (secondYRangeEnd <= firstYRangeEnd && secondYRangeEnd >= firstYRangeStart)
//					|| (firstYRangeStart >= secondYRangeStart && firstYRangeEnd <= secondYRangeEnd)) {
//			retValue = 2;
//			}
//			
//		}
//		if(firstXRangeStart <= secondXRangeEnd && firstXRangeEnd > secondXRangeStart+secondwidth/2){
//			//Log.i("Blocked",""+firstYRangeStart+"/"+firstYRangeEnd+"-"+secondYRangeStart+"/"+secondYRangeEnd);
//			if ((firstYRangeStart <= secondYRangeStart && firstYRangeEnd >= secondYRangeStart)
//					|| (secondYRangeEnd <= firstYRangeEnd && secondYRangeEnd >= firstYRangeStart)
//					|| (firstYRangeStart >= secondYRangeStart && firstYRangeEnd <= secondYRangeEnd)) {
//			retValue = 3;
//			}
//			
//		}
		
		if(secondYRangeEnd >= firstYRangeStart && firstYRangeEnd > secondYRangeEnd){
			//Log.i("Blocked",""+firstYRangeStart+"/"+firstYRangeEnd+"-"+secondYRangeStart+"/"+secondYRangeEnd);
			if ((firstXRangeStart <= secondXRangeStart && firstXRangeEnd > secondXRangeStart+3)
				|| (secondXRangeEnd <= firstXRangeEnd && secondXRangeEnd -3 > firstXRangeStart)
				|| (firstXRangeStart >= secondXRangeStart && firstXRangeEnd <= secondXRangeEnd)) {
			retValue = 4;
			}
			
		}
			//if ((secondYRangeStart >= firstYRangeStart && secondYRangeStart <= firstYRangeEnd)
			//		|| (secondYRangeEnd >= firstYRangeStart && secondYRangeEnd <= firstYRangeEnd)) {
			//	retValue = 3;
			//}
		return retValue;
	}
	
	
	private int checkCollisionBottom(Character first, Character second) {
		int retValue = 0;
		int firstwidth = first.getGraphic().getWidth();
		int firstheight = first.getGraphic().getHeight();
		int secondwidth = second.getGraphic().getWidth();
		int secondheight = second.getGraphic().getHeight();
		int firstYRangeStart = first.getCoordinates().getY();
		int firstYRangeEnd = first.getCoordinates().getY() + firstheight;
		int firstXRangeStart = first.getCoordinates().getX();
		int firstXRangeEnd = first.getCoordinates().getX() + firstwidth;
		int secondYRangeStart = second.getCoordinates().getY();
		int secondYRangeEnd = second.getCoordinates().getY()+ secondheight;
		int secondXRangeStart = second.getCoordinates().getX();
		int secondXRangeEnd = second.getCoordinates().getX()+ secondwidth;
	

		
//		if(secondXRangeStart <= firstXRangeEnd && firstXRangeStart < secondXRangeEnd-secondwidth/2){
//			//Log.i("Blocked",""+firstYRangeStart+"/"+firstYRangeEnd+"-"+secondYRangeStart+"/"+secondYRangeEnd);
//			if ((firstYRangeStart <= secondYRangeStart && firstYRangeEnd >= secondYRangeStart)
//					|| (secondYRangeEnd <= firstYRangeEnd && secondYRangeEnd >= firstYRangeStart)
//					|| (firstYRangeStart >= secondYRangeStart && firstYRangeEnd <= secondYRangeEnd)) {
//			retValue = 2;
//			}
//			
//		}
//		if(firstXRangeStart <= secondXRangeEnd && firstXRangeEnd > secondXRangeStart+secondwidth/2){
//			//Log.i("Blocked",""+firstYRangeStart+"/"+firstYRangeEnd+"-"+secondYRangeStart+"/"+secondYRangeEnd);
//			if ((firstYRangeStart <= secondYRangeStart && firstYRangeEnd >= secondYRangeStart)
//					|| (secondYRangeEnd <= firstYRangeEnd && secondYRangeEnd >= firstYRangeStart)
//					|| (firstYRangeStart >= secondYRangeStart && firstYRangeEnd <= secondYRangeEnd)) {
//			retValue = 3;
//			}
//			
//		}
		if(secondYRangeStart <= firstYRangeEnd && firstYRangeStart < secondYRangeStart){
			//Log.i("Blocked",""+firstYRangeStart+"/"+firstYRangeEnd+"-"+secondYRangeStart+"/"+secondYRangeEnd);
			if ((firstXRangeStart <= secondXRangeStart && firstXRangeEnd > secondXRangeStart+3)
				|| (secondXRangeEnd <= firstXRangeEnd && secondXRangeEnd -10 > firstXRangeStart)
				|| (firstXRangeStart >= secondXRangeStart && firstXRangeEnd <= secondXRangeEnd)) {
			retValue = 1;
			}
			
		}
		//if(secondYRangeEnd >= firstYRangeStart && firstYRangeEnd > secondYRangeEnd){
			//Log.i("Blocked",""+firstYRangeStart+"/"+firstYRangeEnd+"-"+secondYRangeStart+"/"+secondYRangeEnd);
			//if ((firstXRangeStart <= secondXRangeStart && firstXRangeEnd > secondXRangeStart+3)
			//	|| (secondXRangeEnd <= firstXRangeEnd && secondXRangeEnd -3 > firstXRangeStart)
			//	|| (firstXRangeStart >= secondXRangeStart && firstXRangeEnd <= secondXRangeEnd)) {
			//retValue = 4;
			//}
			
		//}
			//if ((secondYRangeStart >= firstYRangeStart && secondYRangeStart <= firstYRangeEnd)
			//		|| (secondYRangeEnd >= firstYRangeStart && secondYRangeEnd <= firstYRangeEnd)) {
			//	retValue = 3;
			//}
		return retValue;
	}
	
	private int checkCollisionSides(Character first, Character second) {
		int retValue = 0;
		int firstwidth = first.getGraphic().getWidth();
		int firstheight = first.getGraphic().getHeight();
		int secondwidth = second.getGraphic().getWidth();
		int secondheight = second.getGraphic().getHeight();
		int firstYRangeStart = first.getCoordinates().getY();
		int firstYRangeEnd = first.getCoordinates().getY() + firstheight;
		int firstXRangeStart = first.getCoordinates().getX();
		int firstXRangeEnd = first.getCoordinates().getX() + firstwidth;
		int secondYRangeStart = second.getCoordinates().getY();
		int secondYRangeEnd = second.getCoordinates().getY()+ secondheight;
		int secondXRangeStart = second.getCoordinates().getX();
		int secondXRangeEnd = second.getCoordinates().getX()+ secondwidth;
	

		
		if(secondXRangeStart <= firstXRangeEnd && firstXRangeStart < secondXRangeEnd-secondwidth/2){
			//Log.i("Blocked",""+firstYRangeStart+"/"+firstYRangeEnd+"-"+secondYRangeStart+"/"+secondYRangeEnd);
			if ((firstYRangeStart <= secondYRangeStart && firstYRangeEnd > secondYRangeStart+3)
					|| (secondYRangeEnd <= firstYRangeEnd && secondYRangeEnd >= firstYRangeStart)
					|| (firstYRangeStart >= secondYRangeStart && firstYRangeEnd <= secondYRangeEnd)) {
			retValue = 2;
			}
			
		}
		if(firstXRangeStart <= secondXRangeEnd && firstXRangeEnd > secondXRangeStart+secondwidth/2){
			//Log.i("Blocked",""+firstYRangeStart+"/"+firstYRangeEnd+"-"+secondYRangeStart+"/"+secondYRangeEnd);
			if ((firstYRangeStart <= secondYRangeStart && firstYRangeEnd > secondYRangeStart+3)
					|| (secondYRangeEnd <= firstYRangeEnd-10 && secondYRangeEnd >= firstYRangeStart)
					|| (firstYRangeStart >= secondYRangeStart && firstYRangeEnd <= secondYRangeEnd)) {
			retValue = 3;
			}
			
			if(firstXRangeStart <= frontedge.getCoordinates().getX()+3){
				retValue = 3;
			}
			
		}
		
			
		return retValue;
	}

	//------------------------------------------------------------------------------------

	@Override
	public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2, int arg3) {
		// TODO Auto-generated method stub

	}

	// //----------------------ON TOUCH EVENT
	// ------------------------------------------------------------
	@Override
	public boolean onTouchEvent(MotionEvent event) {
		synchronized (_thread.getSurfaceHolder()) {
			if (event.getAction() == MotionEvent.ACTION_DOWN) {
				
					_x = (int) event.getX();
					_y = (int) event.getY();
					
				}
			
			
			if (_x < getWidth() / 2) {
				touchPadVisable = true;
				if (event.getAction() == MotionEvent.ACTION_MOVE) {

					for (int i = 0; i < event.getPointerCount(); i++) {
						int _x2 = (int) event.getX(i);
						int _y2 = (int) event.getY(i);

						if (_x2 < getWidth() / 2) {
							
							if (_x2 > _x + 20 && _x2 < _x + 40) {
								moveRight = true;
								moveLeft = false;
								moveRightFast = false;
								moveLeftFast = false;
								//moveUp = false;
								Log.i("Move", "Moving Right");
								if ((_y2 > _y + 20)) {
									// moveUp = true;
									// moveDown = false;
								} else if (_y2 < _y - 20) {
									//moveUp = false;
									// moveDown = true;
								} else {
									//moveUp = false;
									// moveDown = false;
								}
								hen.changeGraphic(BitmapFactory
										.decodeResource(getResources(),
												R.drawable.chickenright));
							}
							if (_x2 > _x + 40) {
								moveRight = false;
								moveLeft = false;
								moveRightFast = true;
								moveLeftFast = false;
								//moveUp = false;
								Log.i("Move", "Moving Right");
								if ((_y2 > _y + 20)) {
									// moveUp = true;
									// moveDown = false;
								} else if (_y2 < _y - 20) {
									//moveUp = false;
									// moveDown = true;
								} else {
									//moveUp = false;
									// moveDown = false;
								}
								hen.changeGraphic(BitmapFactory
										.decodeResource(getResources(),
												R.drawable.chickenright));
							}
							
							
							
							if (_x2 < _x - 20 && _x2 > _x -40){
								Log.i("Move", "Moving Left");
								moveLeft = true;
								moveRight = false;
								moveRightFast = false;
								moveLeftFast = false;
								//moveUp = false;
								if ((_y2 > _y + 20)) {
									// moveUp = true;
									// moveDown = false;
								} else if (_y2 < _y - 20) {
									// moveUp = false;
									// moveDown = true;
								} else {
									// moveUp = false;
									// moveDown = false;
								}
								hen.changeGraphic(BitmapFactory.decodeResource(
										getResources(), R.drawable.chickenleft));
							}

							if (_x2 < _x - 40){
								Log.i("Move", "Moving Left");
								moveLeft = false;
								moveRight = false;
								moveRightFast = false;
								moveLeftFast = true;
								//moveUp = false;
								if ((_y2 > _y + 20)) {
									// moveUp = true;
									// moveDown = false;
								} else if (_y2 < _y - 20) {
									// moveUp = false;
									// moveDown = true;
								} else {
									// moveUp = false;
									// moveDown = false;
								}
								hen.changeGraphic(BitmapFactory.decodeResource(
										getResources(), R.drawable.chickenleft));
							}
							if (_y2 > _y + 20) {
								Log.i("Move", "Moving Down");
								// moveDown = true;
								//moveUp = false;
								if ((_x2 > _x + 20)) {
									moveLeft = false;
									//moveRight = true;
								} else if (_x2 < _x - 20) {
									//moveLeft = true;
									moveRight = false;
								} else {
									moveLeft = false;
									moveRight = false;
								}
							}
							if (_y2 < _y - 20) {
								Log.i("Move", "Moving Up");
								//moveUp = false;
								//moveDown = false;
								if ((_x2 > _x + 20)) {
									moveLeft = false;
									//moveRight = true;
								} else if (_x2 < _x - 20) {
									//moveLeft = true;
									moveRight = false;
								} else {
									moveLeft = false;
									moveRight = false;
								}
							}
							
							
							
						} else {
							if(onground || blockedbottom){
							moveUp = true;
							}
						}

					}
					
				}
			}
				if(_x > getWidth()/2){
					if(onground || blockedbottom){
						moveUp = true;
						}
				}
				if (event.getAction() == MotionEvent.ACTION_UP) {
					touchPadVisable = false;
					//moveUp = false;
					// moveDown = false;
					moveLeftFast = false;
					moveRightFast = false;
					moveLeft = false;
					moveRight = false;
				}

			}
			return true;
		}
	

	@Override
	public void surfaceCreated(SurfaceHolder holder) {
		_thread.setRunning(true);
		if (!_thread.isAlive()) {
			_thread = new MoveTestThread(holder, this);
		}
		_thread.setRunning(true);
		_thread.start();
	}


	@Override
	public void surfaceDestroyed(SurfaceHolder holder)  {
		boolean retry = true;
		_thread.setRunning(false);
		while (retry) {
				_thread.interrupt();
			retry = false;
		}
	}

}
